using HKB.Common;
using HKB.Data.Dal;
using HKB.Data.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
namespace HKB.Data.Bll
{
	public class UserCardCategory
	{
		private readonly HKB.Data.Dal.UserCardCategory dal = new HKB.Data.Dal.UserCardCategory();
		private static UserCardCategory _instance = null;
		public static UserCardCategory instance
		{
			get
			{
				if (UserCardCategory._instance == null)
				{
					UserCardCategory._instance = new UserCardCategory();
				}
				return UserCardCategory._instance;
			}
		}
		public bool Exists(string CardCategory)
		{
			return this.dal.Exists(CardCategory);
		}
		public bool Add(HKB.Data.Model.UserCardCategory model)
		{
			return this.dal.Add(model);
		}
		public bool Update(HKB.Data.Model.UserCardCategory model)
		{
			return this.dal.Update(model);
		}
		public bool Delete(string CardCategory)
		{
			return this.dal.Delete(CardCategory);
		}
		public bool DeleteList(string CardCategorylist)
		{
			return this.dal.DeleteList(CardCategorylist);
		}
		public HKB.Data.Model.UserCardCategory GetModel(string CardCategory)
		{
			return this.dal.GetModel(CardCategory);
		}
		public HKB.Data.Model.UserCardCategory GetModelByCache(string CardCategory)
		{
			string cacheKey = "UserCardCategoryModel-" + CardCategory;
			object obj = DataCache.GetCache(cacheKey);
			if (obj == null)
			{
				try
				{
					obj = this.dal.GetModel(CardCategory);
					if (obj != null)
					{
						int configInt = ConfigHelper.GetConfigInt("ModelCache");
						DataCache.SetCache(cacheKey, obj, System.DateTime.Now.AddMinutes((double)configInt), System.TimeSpan.Zero);
					}
				}
				catch
				{
				}
			}
			return (HKB.Data.Model.UserCardCategory)obj;
		}
		public DataSet GetList(string strWhere)
		{
			return this.dal.GetList(strWhere);
		}
		public DataSet GetList(int Top, string strWhere, string filedOrder)
		{
			return this.dal.GetList(Top, strWhere, filedOrder);
		}
		public System.Collections.Generic.List<HKB.Data.Model.UserCardCategory> GetModelList(string strWhere)
		{
			DataSet list = this.dal.GetList(strWhere);
			return this.DataTableToList(list.Tables[0]);
		}
		public System.Collections.Generic.List<HKB.Data.Model.UserCardCategory> DataTableToList(DataTable dt)
		{
			System.Collections.Generic.List<HKB.Data.Model.UserCardCategory> list = new System.Collections.Generic.List<HKB.Data.Model.UserCardCategory>();
			int count = dt.Rows.Count;
			if (count > 0)
			{
				for (int i = 0; i < count; i++)
				{
					HKB.Data.Model.UserCardCategory userCardCategory = this.dal.DataRowToModel(dt.Rows[i]);
					if (userCardCategory != null)
					{
						list.Add(userCardCategory);
					}
				}
			}
			return list;
		}
		public DataSet GetAllList()
		{
			return this.GetList("");
		}
		public int GetRecordCount(string strWhere)
		{
			return this.dal.GetRecordCount(strWhere);
		}
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			return this.dal.GetListByPage(strWhere, orderby, startIndex, endIndex);
		}
		public DataSet GetListRowid(int Top, string strWhere, string filedOrder)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select ");
			if (Top > 0)
			{
				stringBuilder.Append(" top " + Top.ToString());
			}
			stringBuilder.Append(" ROW_NUMBER() OVER (ORDER BY CardCategory ASC) rowid,CardCategory,CardCategoryName,Remark ");
			stringBuilder.Append(" FROM MBF_UserCardCategory ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			stringBuilder.Append(" order by " + filedOrder);
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
		public DataTable GetDropDownListData(string strWhere)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select CardCategory FieldValue,CardCategoryName FieldText");
			stringBuilder.Append(" FROM MBF_UserCardCategory ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]).Tables[0];
		}
		public string GetNameByCategory(object Category)
		{
			string text = (Category == null) ? "" : Category.ToString();
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select top 1 CardCategoryName FROM MBF_UserCardCategory ");
			if (text.Trim() != "")
			{
				stringBuilder.AppendFormat(" where CardCategory='{0}'", text);
			}
			object single = DbHelperSQL.GetSingle(stringBuilder.ToString());
			string result;
			if (single == null)
			{
				result = "";
			}
			else
			{
				result = single.ToString();
			}
			return result;
		}
	}
}
